package com.datang.litejvm.util;

/**
 * @author: 顶风少年
 * @Description: 字节数组工具类
 * @date: 13:56 2022/6/10
 **/
public class Util {

    /**
     * @author: 顶风少年
     * @Description: 将字节数组变成数字
     * @date: 13:49 2022/6/10
     **/
    public static int byteToInt(byte[] codes) {
        int result = 0;
        if (codes.length == 4) {
            int a = (codes[0] ) << 24;
            int b = (codes[1]) << 16;
            int c = (codes[2]) << 8;
            int d = (codes[3]) << 0;
            result = a | b | c | d;
        } else if (codes.length == 3) {
            int b = (codes[0] ) << 16;
            int c = (codes[1] ) << 8;
            int d = (codes[2] ) << 0;
            result = b | c | d;
        } else if (codes.length == 2) {
            int c = (codes[0]) << 8;
            int d = (codes[1] ) << 0;
            result = c | d;
        } else if (codes.length == 1) {
            int d = (codes[0]) << 0;
            result = d;
        }
        return result;

    }


    /**
     * @author: 顶风少年
     * @Description: 将字节转换成16进制
     * @date: 11:45 2022/6/9
     **/
    public static String byteToHexString(byte[] codes) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < codes.length; i++) {
            byte b = codes[i];
            int value = b & 0xFF;
            String strHex = Integer.toHexString(value);
            if (strHex.length() < 2) {
                strHex = "0" + strHex;
            }
            buffer.append(strHex);
        }
        return buffer.toString();
    }

    /**
     * @author: 顶风少年
     * @Description: 二进制数组转字符串
     * @date: 15:51 2022/6/11
     **/
    public static String byetToString(byte[] codes) {
        String s = new String(codes);
        return s;
    }


}
